<?php

/*
 * 角色权限模型
 * @author turtle
 * create_time 2017-01-02
 */

namespace App\Admin\Models;

use Illuminate\Database\Eloquent\Model;

class PermissionRole extends Model {
    protected $table = 'admin_permission_role';

    /*
     * 添加角色权限
     * @author turtle
     * create_time 2017-01-02
     */

    public static function addPermissionRole($permissions, $role_id) {

        foreach ($permissions as $key=>$permission_id) {
            $data[$key]['permission_id'] = intval($permission_id);
            $data[$key]['role_id'] = $role_id;
        }
        return self::insert($data);
    }

    /* 编辑角色权限
     * @author turtle
     * create_time 2017-01-03
     */

    public static function editPermissionRole($permissions, $role_id) {
        self::where('role_id', $role_id)->delete();
        foreach ($permissions as $key=>$permission_id) {
            $data[$key]['permission_id'] = intval($permission_id);
            $data[$key]['role_id'] = $role_id;
        }
        return self::insert($data);

    }

    /*
     * 获取某一角色下的权限信息
     * @author turtle
     * create_time 2017-01-03
     */

    public static function getPermissionInfo($role_id) {
        $permission_ids = self::where('role_id', $role_id)->select('permission_id')->get();
        foreach ($permission_ids as $k=>$permission) {
            $permissions[$k] = $permission->permission_id;
        }
        return $permissions;

    }
}
